package com.aduerr.v1shouyin.mvp.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.AppDetailEntity;
import com.aduerr.v1shouyin.mvp.adpter.NetworkAdapter;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.Utils;
import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;
import java.util.List;

/**
 * 网络诊断
 * Created by fyf on 2018/4/23 0023.
 */

public class NetworkActivity extends ToolbarBaseActivity {

    public static final String TAG = "NetworkActivity";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_network)
    RecyclerView rlNetwork;
    @BindView(R.id.web_network)
    WebView webNetwork;

    @Override
    protected int contentViewLayoutRes() {
        return R.layout.activity_network;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        Log.e(TAG, "initView: " + AppUtils.getAppName());
        if(NetworkUtils.isAvailableByPing()) {
            initWebView("https://cdn.dns-detect.alicdn.com/https/doc.html");
        }
        Log.e(TAG, "initView: " + NetworkUtils.getDataEnabled());
        List<AppDetailEntity> list = new ArrayList<>();
        rlNetwork.setLayoutManager(new LinearLayoutManager(this));
        String title[] = {"设备厂商", "设备MAC地址", "设备ID", "设备系统版本号", "APP包名",
                "APP名字", "APP版本号", "APP版本码", "APP是否是Debug版本", "APP的签名", "APP签名的SHA1值",
                "设备网络信息", "网络是否连接", "当前网络是否可用", "判断WIFI是否打开",
                "当前WIFI是否连接", "当前WIFI数据是否可用", "当前设备的网络运营商", "获取当前网络连接类型", "当前IP" +
                "地址（不用IPv4）", "当前IP地址（用IPv4）", "设备号"};
        String deviceid = Hawk.get("DeviceID");

        if(!TextUtils.isEmpty(deviceid)) {
            String appDetail[] = {DeviceUtils.getManufacturer(),
                    DeviceUtils.getMacAddress(),
                    DeviceUtils.getAndroidID(),
                    String.valueOf(DeviceUtils.getSDKVersion()),
                    AppUtils.getAppPackageName(), AppUtils.getAppName(),
                    String.valueOf(AppUtils.getAppVersionName()),
                    String.valueOf(AppUtils.getAppVersionCode()),
                    String.valueOf(AppUtils.isAppDebug()),
                    String.valueOf(AppUtils.getAppSignature()),
                    AppUtils.getAppSignatureSHA1(),
                    String.valueOf(((ConnectivityManager) Utils.getApp().getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo()),
                    String.valueOf(NetworkUtils.isConnected()),
                    String.valueOf(NetworkUtils.isAvailableByPing()),
                    String.valueOf(NetworkUtils.getWifiEnabled()),
                    String.valueOf(NetworkUtils.isWifiConnected()),
                    String.valueOf(NetworkUtils.isWifiAvailable()),
                    NetworkUtils.getNetworkOperatorName(),
                    String.valueOf(NetworkUtils.getNetworkType()),
                    NetworkUtils.getIPAddress(false),
                    NetworkUtils.getIPAddress(true), Hawk.get("DeviceID")};
            for(int i = 0; i < title.length; i++) {
                AppDetailEntity entity = new AppDetailEntity();
                entity.setTitle(title[i]);
                entity.setApp_msg(appDetail[i]);
                list.add(entity);
            }
        } else {
            String appDetail[] = {DeviceUtils.getManufacturer(),
                    DeviceUtils.getMacAddress(),
                    DeviceUtils.getAndroidID(),
                    String.valueOf(DeviceUtils.getSDKVersion()),
                    AppUtils.getAppPackageName(), AppUtils.getAppName(),
                    String.valueOf(AppUtils.getAppVersionName()),
                    String.valueOf(AppUtils.getAppVersionCode()),
                    String.valueOf(AppUtils.isAppDebug()),
                    String.valueOf(AppUtils.getAppSignature()),
                    AppUtils.getAppSignatureSHA1(),
                    String.valueOf(((ConnectivityManager) Utils.getApp().getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo()),
                    String.valueOf(NetworkUtils.isConnected()),
                    String.valueOf(NetworkUtils.isAvailableByPing()),
                    String.valueOf(NetworkUtils.getWifiEnabled()),
                    String.valueOf(NetworkUtils.isWifiConnected()),
                    String.valueOf(NetworkUtils.isWifiAvailable()),
                    NetworkUtils.getNetworkOperatorName(),
                    String.valueOf(NetworkUtils.getNetworkType()),
                    NetworkUtils.getIPAddress(false),
                    NetworkUtils.getIPAddress(true), "无"};
            for(int i = 0; i < title.length; i++) {
                AppDetailEntity entity = new AppDetailEntity();
                entity.setTitle(title[i]);
                entity.setApp_msg(appDetail[i]);
                list.add(entity);
            }
        }


        NetworkAdapter networkAdapter = new NetworkAdapter(NetworkActivity.this, list);
        rlNetwork.setAdapter(networkAdapter);
    }

    /**
     * 设置WebView
     *
     * @param url
     */
    @SuppressLint("SetJavaScriptEnabled")
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void initWebView(String url) {
        WebSettings settings = webNetwork.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);// LOAD_NO_CACHE
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(true);
        settings.setDomStorageEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
//        slideWeb.setWebViewClient(Tools.getWebViewFinish());
        //加载需要显示的网页
        webNetwork.loadUrl(url);

    }

    @Override
    protected void initToolbar() {
        super.initToolbar();
        tvTitle.setText("网络诊断");
    }
}
